<template>
  <PageWrapper contentFullHeight fixedHeight>
    <VxeBasicTable class="w-full h-full" ref="tableRef" v-bind="gridOptions">
      <template #action="{ row }">
        <a-space>
          <a-tooltip placement="bottom">
            <template #title>
              <span>编辑</span>
            </template>
            <a-button type="link" size="small" @click="handleEdit(row)">
              <template #icon><Icon icon="clarity:note-edit-line" /></template>
            </a-button>
          </a-tooltip>
          <a-tooltip placement="bottom">
            <template #title>
              <span>删除此配置</span>
            </template>
            <a-button type="link" danger size="small">
              <template #icon>
                <a-popconfirm placement="left" title="是否确认删除？" ok-text="确定" cancel-text="取消" @confirm="handleDelete(row)">
                  <Icon icon="ant-design:delete-outlined" />
                </a-popconfirm>
              </template>
            </a-button>
          </a-tooltip>
        </a-space>
      </template>
    </VxeBasicTable>
    <ConfigDrawer @register="registerDrawer" @success="reloadTableData" />
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { ref, reactive } from 'vue';
  import { message } from "ant-design-vue";
  import { Icon } from '@gui-pkg/components';
  import { PageWrapper, BasicTableProps, useDrawer } from '@gui-pkg/antdcomponents';
  import { VxeBasicTable, VxeGridInstance } from '@gui-pkg/antdcomponents';
  import { columns, searchFormSchema } from './config.data';
  import {getTargetList, deltargetData} from '@/api/alerts/warningConfig';
  import ConfigDrawer from './ConfigDrawer.vue';

  defineOptions({ name: 'WarningSettings' });

  const [registerDrawer, { openDrawer }] = useDrawer();
  const tableRef = ref<VxeGridInstance>();
  const gridOptions = reactive<BasicTableProps>({
    id: tableRef.value,
    keepSource: true,
    editConfig: { trigger: 'click', mode: 'cell', showStatus: true },
    columns: columns,
    treeConfig: {
      childrenField: 'children',
      indent: 20,
      showIcon: true,
      iconOpen: 'vxe-icon-square-minus',
      iconClose: 'vxe-icon-square-plus'
    },
    toolbarConfig: {
      className: "vxe-toolbarConfig",
      buttons: [
        {
          content: '新增配置',
          buttonRender: {
            name: 'AButton',
            props: {
              type: 'primary',
              preIcon: 'grommet-icons:add',
            },
            events: {
              click: () => handleCreate(),
            },
          },
        },
      ],
    },
    formConfig: {
      enabled: true,
      className: "vxe-formConfig",
      items: searchFormSchema,
    },
    height: 'auto',
    proxyConfig: {
      seq: true,
      ajax: {
        query: async ({ page, form }) => {
          const tableListData = await getTargetList({
            pageNum: page.currentPage,
            pageSize: page.pageSize,
            ...form,
          });
          return { list: tableListData.records,total:tableListData.total };
        },
      },
    },
  });

  function handleCreate() {
    openDrawer(true, {
      isUpdate: false,
    });
  }

  // 编辑配置
  function handleEdit(row) {
    openDrawer(true, {
      record: row,
      isUpdate: true,
    });
  }

  // 删除此配置
  const handleDelete = async (row)=> {
    await deltargetData(row.id);
    reloadTableData();
    message.success('删除成功');
  }

  // 重新请求菜单表格数据
  const reloadTableData = async () => {
    const tableListData = await getTargetList({pageNum: 1, pageSize: 20 });
    tableRef.value.loadData(tableListData.records);
  };
</script>

